<script setup lang="ts">
import type { CascaderProps } from 'ant-design-vue'
import { ref, shallowRef } from 'vue'
import { getPurchaseListAll } from '@/api/purchase/purchase-list.ts'

// 添加一个方法来触发自定义事件，传递地址信息给父组件
const emit = defineEmits(['purchaseSelected'])

function purchaseSelected() {
  console.log(`purchaseSelected ${value.value[1]}`)

  emit('purchaseSelected', value.value[1])
}

const value = ref<string[]>([])
let options = shallowRef<CascaderProps['options']>([])

async function init() {
  console.log('进入到初始采购单列表函数中')
  // 初始加载所有产品
  const mes = await getPurchaseListAll()

  const arrayOfObjects = JSON.parse(mes.data)

  if (Array.isArray(arrayOfObjects)) {
    options.value = arrayOfObjects
  } else {
    console.error('getPurchaseListAll 返回的数据不是数组:', mes.data)
  }
}

onMounted(() => {
  init()
})
</script>

<template>
  <a-cascader
      v-model:value="value"
      :field-names="{ label: 'label', value: 'code', children: 'items' }"
      expand-trigger="hover"
      :options="options"
      placeholder="请选择采购单"
      allow-clear
      @change="purchaseSelected"
  />
</template>

<style scoped lang="less">

</style>
